using DataWorks_BLL;
using DataWorks_DAL;
using DataWorks_IBLL;
using DataWorks_IDAL;
using DataWorks_Sqlite;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Serilog;
using Yitter.IdGenerator;

var builder = WebApplication.CreateBuilder(args);
var configstr = new ConfigurationBuilder()
                 .SetBasePath(Directory.GetCurrentDirectory())
                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                 .Build();
string? sqlconfig = configstr.GetConnectionString("DataWorks_SqliteConnection");
string[]? ipstr = configstr.GetSection("Localhost").Value?.Split(",");
IdGeneratorOptions options = new IdGeneratorOptions(1);
YitIdHelper.SetIdGenerator(options);
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configstr)
    .CreateLogger();

builder.Host.UseSerilog(Log.Logger, true);
builder.Services.AddDbContext<MyDbContext>(options =>
{
    if(sqlconfig!=null)
    options.UseSqlite(sqlconfig);
});

builder.Services.AddSerilog();
builder.Services.AddCors(opt =>
{

    opt.AddPolicy("react", p =>
    {
        if (ipstr != null)
            p.AllowAnyHeader().AllowAnyMethod().WithOrigins(ipstr).AllowCredentials(); ;
    });
});
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, option =>
    {

        option.Cookie.SameSite = SameSiteMode.None; // ����SameSite����
        option.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        option.AccessDeniedPath = "/Login/NoAuthResult";


    });
builder.Services.AddScoped<DbContext, MyDbContext>();
builder.Services.AddTransient<IDAL_Login, DAL_Login>();
builder.Services.AddTransient<IBLL_Login, BLL_Login>();
builder.Services.AddTransient<IDAL_Props_Clean, DAL_Props_Clean>();
builder.Services.AddTransient<IBLL_Props_Clean, BLL_Props_Clean>();

builder.Services.AddTransient<IDAL_Props_DataPredict, DAL_Props_DataPredict>();
builder.Services.AddTransient<IBLL_Props_DataPredict, BLL_Props_DataPredict>();

builder.Services.AddTransient<IDAL_Props_LinearConvert, DAL_Props_LinearConvert>();
builder.Services.AddTransient<IBLL_Props_LinearConvert, BLL_Props_LinearConvert>();


builder.Services.AddTransient<IDAL_Props_HalfHourCombine, DAL_Props_HalfHourCombine>();
builder.Services.AddTransient<IBLL_Props_HalfHourCombine, BLL_Props_HalfHourCombine>();
builder.Services.AddTransient<IDAL_Props_SpikeRemove, DAL_Props_SpikeRemove>();
builder.Services.AddTransient<IBLL_Props_SpikeRemove, BLL_Props_SpikeRemove>();
builder.Services.AddTransient<IDAL_Props_Admin, DAL_Props_Admin>();
builder.Services.AddTransient<IBLL_Props_Admin, BLL_Props_Admin>();

builder.Services.AddTransient<IDAL_Props_TxtCombine, DAL_Props_TxtCombine>();
builder.Services.AddTransient<IBLL_Props_TxtCombine, BLL_Props_TxtCombine>();
builder.Services.AddTransient<IDAL_Props_MileageCombine, DAL_Props_MileageCombine>();
builder.Services.AddTransient<IBLL_Props_MileageCombine, BLL_Props_MileageCombine>();

builder.Services.AddTransient<IDAL_Props_MileageCalculation, DAL_Props_MileageCalculation>();
builder.Services.AddTransient<IBLL_Props_MileageCalculation, BLL_Props_MileageCalculation>();

builder.Services.AddTransient<IDAL_Props_ConvertCSVI, DAL_Props_ConvertCSVI>();
builder.Services.AddTransient<IBLL_Props_ConvertCSVI, BLL_Props_ConvertCSVI>();

builder.Services.AddTransient<IDAL_Props_Unify, DAL_Props_Unify>();
builder.Services.AddTransient<IBLL_Props_Unify, BLL_Props_Unify>();


builder.Services.AddTransient<IDAL_Props_User_HalfHourImport, DAL_Props_User_HalfHourImport>();
builder.Services.AddTransient<IBLL_Props_User_HalfHourImport, BLL_Props_User_HalfHourImport>();
builder.Services.AddTransient<IDAL_Props_User_MileageImport, DAL_Props_User_MileageImport>();
builder.Services.AddTransient<IBLL_Props_User_MileageImport, BLL_Props_User_MileageImport>();

builder.Services.AddTransient<IDAL_Props_LSTMPredict, DAL_Props_LSTMPredict>();
builder.Services.AddTransient<IBLL_Props_LSTMPredict, BLL_Props_LSTMPredict>();

builder.Services.AddTransient<IDAL_Props_DbcMessageTransfer, DAL_Props_DbcMessageTransfer>();
builder.Services.AddTransient<IBLL_Props_DbcMessageTransfer, BLL_Props_DbcMessageTransfer>();


builder.Services.AddTransient<IDAL_SystemVersion, DAL_SystemVersion>();
builder.Services.AddTransient<IBLL_SystemVersion, BLL_SystemVersion>();
builder.Services.AddMemoryCache();
builder.Services.AddScoped<MemoryCache>();
builder.Services.AddControllers();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseSerilogRequestLogging();
app.UseCors("react");
app.UseStaticFiles();
//app.UseCookiePolicy();
app.UseAuthentication();

app.UseAuthorization();

app.MapControllers();

app.Run();
